package com.sikao.app.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.ChangePhoneNumberSubmitBean;
import com.sikao.app.bean.GetSmsSubmitBean;
import com.sikao.app.dao.ChangePhoneNumberModel;
import com.sikao.app.dao.SmsCodeModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.StringUtils;
import com.sikao.app.utils.TimeCountUtil;
/**
 * 修改手机号
 */
public class ChangePhoneNumberActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private EditText payPswEt;
	private EditText phoneNumberEt;
	private EditText smsCodeEt;
	private Button sendCodeBtn;
	private Button changePhoneNumberBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_change_phone_number);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		payPswEt = (EditText) findViewById(R.id.pay_psw_et);
		phoneNumberEt = (EditText) findViewById(R.id.phone_number_et);
		smsCodeEt = (EditText) findViewById(R.id.sms_code_et);
		sendCodeBtn = (Button) findViewById(R.id.sms_code_btn);
		changePhoneNumberBtn = (Button) findViewById(R.id.change_phone_number_btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		sendCodeBtn.setOnClickListener(this);
		changePhoneNumberBtn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("修改手机号");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.sms_code_btn:
				getCode();
				break;
			case R.id.change_phone_number_btn:
				changePhoneNumber();
				break;
			default:
				break;
		}
	}

	private void getCode(){
		if (TextUtils.isEmpty(phoneNumberEt.getText().toString())) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return;
		}
		/*if (!StringUtils.isMobile(phoneNumberEt.getText().toString())) {
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return;
		}*/
		GetSmsSubmitBean bean = new GetSmsSubmitBean();
		bean.setType("updatePhone");
		bean.setPhone(phoneNumberEt.getText().toString());
		bean.setId("1");

		new SmsCodeModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean smsBean) {
				if (smsBean.getStatus().equals("1")) {
					Toast.makeText(ChangePhoneNumberActivity.this, "成功发送", Toast.LENGTH_SHORT).show();
					TimeCountUtil timeCountUtil = new TimeCountUtil(60000, 1000, sendCodeBtn);
					timeCountUtil.start();
				} else {
					Toast.makeText(ChangePhoneNumberActivity.this, smsBean.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), true);
	}

	private void changePhoneNumber(){

		if(!checkInput()){
			return;
		}
		ChangePhoneNumberSubmitBean bean = new ChangePhoneNumberSubmitBean();
		bean.setPayPwd(payPswEt.getText().toString());
		bean.setPhone(phoneNumberEt.getText().toString());
		bean.setPhoneCode(smsCodeEt.getText().toString());
		bean.setType("updatePhone");
		bean.setId("1");
		new ChangePhoneNumberModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean data) {
				if (data == null){
					return;
				}
				if (data.getStatus().equals("1")){
					Toast.makeText(ChangePhoneNumberActivity.this, "成功换绑手机号", Toast.LENGTH_SHORT).show();
					finish();
				}else{
					Toast.makeText(ChangePhoneNumberActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean),true);
	}

	private boolean checkInput(){

		if (TextUtils.isEmpty(payPswEt.getText().toString())){
			Toast.makeText(this, "请输入支付密码", Toast.LENGTH_SHORT).show();
			return false;
		}

		if (TextUtils.isEmpty(phoneNumberEt.getText().toString())){
			Toast.makeText(this, "请输入新手机号", Toast.LENGTH_SHORT).show();
			return false;
		}

		/*if (!StringUtils.isMobile(phoneNumberEt.getText().toString())){
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return false;
		}*/

		if (TextUtils.isEmpty(smsCodeEt.getText().toString())){
			Toast.makeText(this, "请输入短信验证码", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
